package bairui.international.weixin.portal.miniapp.controller.order.manage;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.querydsl.QSort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;
import com.querydsl.core.BooleanBuilder;

import bairui.international.model.domain.jpa.QTB_ProductOrder;
import bairui.international.model.domain.jpa.TB_BranchCompanyStaff;
import bairui.international.model.domain.jpa.TB_ProductOrder;
import bairui.international.model.domain.jpa.TB_ProductOrderItem;
import bairui.international.model.domain.jpa.TB_User;
import bairui.international.model.status.State_ProductOrder;
import bairui.international.model.type.Type_ProductOrder;
import bairui.international.service.Service_BranchCompanyStaff;
import bairui.international.service.Service_ProductOrder;
import bairui.international.service.Service_ProductOrderItem;
import bairui.international.service.Service_User;
import bairui.international.weixin.portal.miniapp.controller.order.product.C_Order_Product_ShowImg_Get;
import bairui.international.weixin.portal.miniapp.controller.product.product.C_Product_ShowImg_Get;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 *
 * 列表
 *
 * @generated
 * 
 * @see STest_C_Order_Manage_Page_Get 测试
 *
 */
@Controller
public class C_Order_Manage_Page_Get
 extends SC_Order_Manage_Page_Get 
{
	
	private final QTB_ProductOrder QProductOrder =  QTB_ProductOrder.tB_ProductOrder;
	
	@Autowired private Service_ProductOrder service_ProductOrder;
	@Autowired private Service_User service_User;
	@Autowired private Service_BranchCompanyStaff service_BranchCompanyStaff;
	@Autowired private Service_ProductOrderItem service_ProductOrderItem;
	
	/**
	 * Get请求
	 */
	@Action(Get.class)
	@GetMapping(Get.RequestMappingValue)
	public @FomatOutput @ResponseBody Object get(
		ModelMap map, HttpServletRequest request,
		@RequestParam Integer startPage,
		@RequestParam(required=false) Integer state,
		@RequestParam String skey
	) throws Exception {
		/*
		 * 处理状态
		 */
		BooleanBuilder query = new BooleanBuilder();
		State_ProductOrder orderState = null;
		if(null == state){
			
		}else if(state == 1){
			orderState = State_ProductOrder.Paying;
		}else if(state == 2){
			orderState = State_ProductOrder.Verifing;
		}else if(state == 3){
			orderState = State_ProductOrder.Sending;
		}else if(state == 4){
			orderState = State_ProductOrder.Sended;
		}else if(state == 5){
			orderState = State_ProductOrder.Finished;
		}
		if(null != orderState){
			query.and(QProductOrder.state.eq(orderState));
		}
		/*
		 * 处理类型
		 */
		query.and(QProductOrder.type.eq(Type_ProductOrder.Buy));
		/*
		 * 过滤公司
		 */
		TB_User user = service_User.findOne(it->it.skey.eq(skey));
		TB_BranchCompanyStaff branchCompanyStaff = service_BranchCompanyStaff.getActive(user);
		// 
		query.and(QProductOrder.branchCompany.eq(branchCompanyStaff.getBranchCompany()));
		if(!branchCompanyStaff.getDirectorFlag()){
			query.and(QProductOrder.sender.eq(branchCompanyStaff));
		}
		//
		return service_ProductOrder
				.page(
						it->query,
						PageRequest.of(startPage, 5),
						it->new QSort(it.id.desc())
					)
				.getContent()
				.stream()
				.map(order->new Output(order))
				.collect(Collectors.toList());
	}
	
	private class Output {
		private @Getter Long id;
		private @Getter String number;
		private @Getter Double price;
		private @Getter Double score;
		private @Getter String imgUrl;
		private @Getter State_ProductOrder state;
		private @Getter String stateComment;
		private @Getter String payWay;
		private @Getter String submitDate;
		// 产品
		private @Getter List<ProductOutput> items;
				
		public Output(TB_ProductOrder order){
			this.id = order.getId();
			this.number = order.getNumber();
			this.price = order.getPrice();
			this.score = order.getScore();
			this.imgUrl = new C_Order_Product_ShowImg_Get.Get.Operate() {
				@Override public Object getId() {
					return order.getId();
				}
			}.getUrl();
			this.state = order.getState();
			this.stateComment = order.getState().getComment();
			this.payWay = null== order.getPayWay() ? "" : order.getPayWay().getComment();
			this.submitDate = null==order.getSubmitDate() ? "" : new SimpleDateFormat("yyyy/MM/dd hh:mm:ss").format(order.getSubmitDate());
			// 订单项
			items = service_ProductOrderItem
					.list(it->it.productOrder.eq(order))
					.stream()
					.map(it->new ProductOutput(it))
					.collect(Collectors.toList());
		}
	}
	
	@AllArgsConstructor
	private static class ProductOutput{
		@Getter private Long id;
		@Getter private String name;
		@Getter private String imgUrl;
		
		public ProductOutput(TB_ProductOrderItem orderItem) {
			this.id = orderItem.getProduct().getId();
			this.name = orderItem.getProduct().getName();
			this.imgUrl = new C_Product_ShowImg_Get.Get.Operate() {
				@Override public Object getId() {
					return orderItem.getProduct().getId();
				}
			}.getUrl();
			//
		}
	}

}
